#include "../../framework/framework.h"
#include "../inc/xFont.h"
#include "../inc/xDevice.h"
#include "../inc/xMedia.h"
namespace Sky
{

	TInt xFont::ms_Height = 18;
	TUint xFont::ms_Width = 0;
	TUint xFont::ms_Weight =FW_BOLD;
	TUint xFont::ms_MipLevels = 0;
	TBool xFont::ms_bItalic = vFalse;
	TUint xFont::ms_CharSet =DEFAULT_CHARSET;
	TUint xFont::ms_Precision = OUT_DEFAULT_PRECIS;
	TUint xFont::ms_Quality = DEFAULT_QUALITY;
	TUint xFont::ms_PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	TChar  xFont::ms_FaceName[LF_FACESIZE] = {TEXT("Arial")};

	TVoid xFont::StaticInit( TInt height /*= 12*/,TUint width /*= 0*/,TUint weight /*= FW_BOLD*/, TUint levels /*= 0*/,
		TBool bItalic /*= vFalse*/,TUint8 charSet /*= DEFAULT_CHARSET*/, TUint8 precision /*= OUT_DEFAULT_PRECIS*/,TUint8 quality /*= DEFAULT_QUALITY*/, 
		TUint8 pitchAndFamily /*= DEFAULT_PITCH | FF_DONTCARE*/,TChar* faceName /*= L("Arial")*/ )
	{
		ms_Height = height;
		ms_Width = width;
		ms_Weight =weight;
		ms_MipLevels = levels;
		ms_bItalic = bItalic;
		ms_CharSet =charSet;
		ms_Precision = precision;
		ms_Quality = quality;
		ms_PitchAndFamily = pitchAndFamily;
		TInt i = 0;
		do
		{
			ms_FaceName[i] = faceName[i];
			i++;
		} while (i<LF_FACESIZE && faceName[i] != 0);
	}

	TVoid xFont::Initialise()
	{
		m_Height = ms_Height;
		m_Weight = ms_Weight;
		m_Width = ms_Width;
		m_MipLevels = ms_MipLevels;
		m_bItalic = ms_bItalic;
		m_CharSet = ms_CharSet;
		m_Precision = ms_Precision;
		m_Quality = ms_Quality;
		m_PitchAndFamily = ms_PitchAndFamily;
		TInt i = 0;
		do
		{
			m_FaceName[i] = ms_FaceName[i];
			i++;
		} while (i<LF_FACESIZE && ms_FaceName[i] != 0);
	}
	xFont::xFont()
	{
		Initialise();
	}

	xFont::~xFont()
	{
		Finalise();
	}

	TResult xFont::onCreate( xDxApp* app )
	{
		return XE_OK;
	}

	TResult xFont::onCreate( xDevice* device )
	{
		TResult hr;
		hr = D3DXCreateFont(device->getDevice9(),m_Height,m_Width,m_Weight,m_MipLevels,m_bItalic,m_CharSet,m_Precision,m_Quality,m_PitchAndFamily,m_FaceName,&m_Res);
		if(FAILED(hr)){
			myDx_Trace_hr0(M,hr,_XT("xFont::onCreate() failed"));
			return XE_APICALL;
		} // if(FAILED(hr))
		return XE_OK;
	}
	TResult xFont::onDelete( xDxApp* app )
	{
		Finalise();
		return XE_OK;
	}

	TResult xFont::onDelete( xDevice* device )
	{
		SAFE_RELEASE(m_Res);
		return XE_OK;
	}
	TResult xFont::onReset( xDevice* device )
	{
		xAssertA(m_Res&&"m_Res == vNull in onReset()");
		myDxV_Ret_hr_M(m_Res->OnResetDevice(),XE_APICALL);
		return XE_OK;
	}

	TResult xFont::onLost( xDevice* device )
	{
		xAssertA(m_Res&&"m_Res == vNull in onReset()");
		myDxV_Ret_hr_M(m_Res->OnLostDevice(),XE_APICALL);
		return XE_OK;
	}

	TVoid xFont::Finalise()
	{
		return ;
	}

	ID3DXFont* xFont::getFont()
	{
		return m_Res;
	}

	TInt xFont::getTextHeight()
	{
		return m_Height;
	}
}